package br.great.DAO;

import java.sql.SQLException;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import br.great.classes.TexteRange;
import br.great.beans.Photo;

/**
* PhotoMap
* Copyright (c) 2007-2008 STEAMER - LIG, All rights reserved.
*/

/**
 * @author Developpement of PhotoMap
 */
public class DAO_Photo {
    private static volatile DAO_Photo instaceDAO_Photo = new DAO_Photo();
    
    /** The private constructor of DAO_Photo for singleton
    */
    private DAO_Photo(){    
    }
    
    /** The method for get the instance of the class DAO_Photo
    * @return DAO_Photo instaceDAO_Photo
    */
    public static DAO_Photo getInstance() {
        //if(instaceDAO_Photo==null)
           synchronized(DAO_Photo.class) {
             if (instaceDAO_Photo == null)
               instaceDAO_Photo = new DAO_Photo();
           }
        return instaceDAO_Photo;
    }
    
    public void insertPhoto(Photo photo,br.great.beans.Address address, int idMultimedia, int idTemperature, int idWeather, int idAddress, int idTime){
        try {

            String insertPhoto = "insert into Photo values(" + idMultimedia + "," + idTemperature + "," + idWeather + "," + idAddress + "," + idTime + ",'" + photo.getName() + "','" + photo.getURIPhoto() + "','" + photo.getCoor().getPoint() + "','" + photo.getURIAnnotations() + "','" + TexteRange.tradeText(address.getAddress()) + "','" + photo.getWeather().getDescription() + "','" + photo.getDateTime() + "','" + photo.getPartOfDay() + "','" + photo.isANNOTATION_VALIDATED() + "','" + photo.getSeason() + "','{300,400}');";

//System.out.println(insertPhoto);
            PgConnect conn = PgConnect.getInstance();
            conn.executeInsert(insertPhoto);
            insertNearbyBluetooth(photo, idMultimedia);
            conn.Close();
        } catch (SQLException ex) {
            Logger.getLogger(DAO_Photo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    
    public void insertNearbyBluetooth(Photo p, int id_multimedia){
        
        Vector<String> devices=p.getDevices();
        
        if(devices==null){
            //no devices in the range
           // System.out.println("No devices in the range");
        }else{//devics found
            for(int j=0;j<devices.size();j++){
                try {
                    String insertNearbyBluetooth = "insert into NEAR_BY_BLUETOOTH values(nextval('seqmax_nearbybt')," + id_multimedia + ",'" + devices.get(j) + "');";

//System.out.println(insertPhoto);
                    PgConnect conn = PgConnect.getInstance();
                    conn.executeInsert(insertNearbyBluetooth);
                    conn.Close();
                } catch (SQLException ex) {
                    Logger.getLogger(DAO_Photo.class.getName()).log(Level.SEVERE, null, ex);
                }
            }    
        }
    }
    
//    public static void main(String[] args){
//        
//        DAO_Photo dp = DAO_Photo.getInstance();
//        Photo p = new Photo();
//        p.setName("grenoble1");
//        p.setURIPhoto("steamer.grenoble1");
//        Point point = new Point();
//        point.setLatitude(12.3);
//        point.setLongitude(23.4);
//        p.setCoor(point);
//        p.setURIAnnotations("Steamer.Annotation.Grenoble1");
//        Temperature temperature = new Temperature();
//        p.setTemperature(temperature);
//        Weather weather = new Weather();
//        weather.setDescription("description Weather");
//        Address address = new Address();
//        address.setNameCity("Paris");
//        address.setNameCountry("France");
//        address.setNameRoad("Rue1");
//        p.setWeather(weather);
//        p.setDateTime(Timestamp.valueOf("2008-02-05 10:22:34"));
//        p.setPartOfDay("Matin");
//        p.setANNOTATION_VALIDATED(false);
//        p.setSeason("Spring");
//        
//        float[] fa = {1,2};
//        p.setLength_width(fa);
//        
//        dp.insertPhoto(p,address, 1, 1, 1, 1, 1);
//    }
}
